package org.ysu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.ysu.common.JsonResult;
import org.ysu.utils.FileUploadInfo;
import org.ysu.utils.FileUploadUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
@RequestMapping("/util")
public class UtilController {

    @Autowired
    private FileUploadInfo fileUploadInfo;
    @Value("${web.upload-path}")
    private String uploadPath;
    @RequestMapping("/uploadPic")
    public JsonResult upload(MultipartFile file, HttpServletRequest request){
        //上传图片
        return FileUploadUtils.upload(file,uploadPath,"image");
    }

    @RequestMapping("/get/image/{data}/{file}")
    public void getFile(@PathVariable String data, @PathVariable String file, HttpServletResponse response){
        String fileUrl = "image"+"/"+data+"/"+file;
        //获取d:temp1下的fileurl文件，并返回
        FileUploadUtils.getFile(fileUrl,fileUploadInfo.getImageBasePath(),response);
    }
}
